WWL.bulletbox = function ( name )
{
	var bulletbox = new WWL ( 'bulletbox', name );

	bulletbox.items = [];

	bulletbox._timing = 3000;
	bulletbox._animating = false;

	bulletbox.set_timing =  function ( mill )
	{
		bulletbox._timing =  mill;
	}
	bulletbox.cbacks [ 'display' ] = function ( info, cback )
	{
		cback ( "DEFINIRE CBACK DISPLAY" );	
	};
		

	bulletbox.add_item = function ( info )
	{
		if ( !info ) return;
		bulletbox.items.push ( info );
	};

	bulletbox.animation_end = function ()
	{
		bulletbox._animating = false;
	};

	bulletbox.to_string =  function () { return '' ;};

	bulletbox.cbacks [ 'render' ]  = function ()
	{
		var _cnt;
		console.debug  ( bulletbox );
		bulletbox.dest_div = $el ( bulletbox._dest_id );
		bulletbox.dest_div.style.overflow = 'hidden';
	
		bulletbox.size = liwe.dom.get_size ( bulletbox.dest_div );
		bulletbox._current = 0;

		bulletbox.container =  document.createElement ( 'div' );
		bulletbox.container.id = 'bulletbox_' +  bulletbox.name;
		bulletbox.container.className = 'bulletbox';
		bulletbox.container._showed = 1;
		liwe.events.add ( bulletbox.container , 'mouseover', function ( ev ) { bulletbox.toggle_controls ( ev );} );
		liwe.events.add ( bulletbox.container , 'mouseover', function ( ev ) { bulletbox._stop_roll( ev );} );
		liwe.events.add ( bulletbox.container , 'mouseout', function ( ev ) { bulletbox.toggle_controls ( ev );} );
		liwe.events.add ( bulletbox.container , 'mouseout', function ( ev ) { bulletbox._start_roll ( ev );} );


		//AGGIUNGERE EVENTI PER MOSTRARE I BULLET BOX
		//OVER APPARE OUT SCOMPARE


		bulletbox.content = document.createElement ( 'div' );
		bulletbox.content.id = 'bulletbox_content_' +  bulletbox.name;
		bulletbox.content.className = 'bulletbox_content';

	

		bulletbox.content.style.width = bulletbox.size [ 0 ] + 'px';
		bulletbox.content.style.height = bulletbox.size [ 1 ] + 'px';
		

		//the 2 div for switching animation
		bulletbox.cnt = [];
		for ( _cnt = 0; _cnt < 2; _cnt ++ )
		{
			var cnt = document.createElement ( 'div' );
			cnt.id =  bulletbox.name + '_cnt_' + _cnt;
			cnt.className = 'bulletbox_cnt';
			cnt.style.width = bulletbox.size [ 0 ] + 'px';
			cnt.style.height = bulletbox.size [ 1 ] + 'px';
			bulletbox.cnt.push ( cnt );

			bulletbox.content.appendChild ( cnt );
		};
		

		//the div for bulletbox
		bulletbox.bb = document.createElement ( 'div');
		bulletbox.bb.id = 'bulletbox_controls_' +  bulletbox.name;
		bulletbox.bb.className = 'bulletbox_controls';

		bulletbox.bullets = document.createElement ( 'div' );
		bulletbox.bullets.className = 'bulletbox_bullets';

		bulletbox.bb.appendChild ( bulletbox.bullets );
		//the bullets
		for ( _cnt = 0; _cnt < bulletbox.items.count(); _cnt++ )
		{
			var bullet = document.createElement ( 'div' );
			bullet.className = 'bullet_item bulletbtn';
			bullet._pos = _cnt;
			bullet.id = 'bullet:' + _cnt;
			if(  _cnt == 0 )
			{
				bulletbox._active =  bullet;
			};
			liwe.events.add ( bullet, 'click', function ( ev ) { bulletbox._click ( ev, this );} );
			bulletbox.bullets.appendChild ( bullet );
		};
	
		var _x = document.createElement ( 'div' )
		_x.style.clear = 'both';
		bulletbox.bullets.appendChild ( _x  );
	


		bulletbox.container.appendChild ( bulletbox.content );
		bulletbox.container.appendChild ( bulletbox.bb );	

		bulletbox.dest_div.appendChild ( bulletbox.container );

		//center bullet
		var bullets_dim = liwe.dom.get_size (  bulletbox.bullets );
		liwe.dom.add_class ( bulletbox._active, "active" );
		bulletbox.bullets.style.margin = '0px 0px 0px ' +  ( ( bullets_dim [ 0 ] / 2 ) * -1 ) + 'px';
		bulletbox.toggle_controls ( null );


	
		bulletbox.cbacks [ 'display' ] ( bulletbox.items [ 0 ], bulletbox.display_item );
		bulletbox._start_roll ();
		bulletbox._pos = 0;	
	
	};

	bulletbox._click = function ( ev, bullet )
	{
		if ( bullet == bulletbox._active || bulletbox._animating ) return;
		clearTimeout ( bulletbox.t_roll );

		if ( bulletbox._active ) liwe.dom.del_class ( bulletbox._active, 'active' );
		liwe.dom.add_class ( bullet, "active" );
		bulletbox._active = bullet;		
	
		bulletbox._pos = bullet._pos;	
		
		var info = bulletbox.items [ bullet._pos ];
		bulletbox.cbacks [ 'display' ] ( info, bulletbox.display_item );
	};
		

	bulletbox.display_item = function ( html, effect )
	{
		var _next, _current;
		if ( typeof ( effect ) == 'undefined' )
		{
			_current = bulletbox.cnt [ 0 ];
			$( _current.id , html );
			return;
		}

		_current = bulletbox.cnt [ bulletbox._current ];
		bulletbox._current = 1- bulletbox._current;
		_next = bulletbox.cnt [ bulletbox._current ];
		
		$( _next.id , html );

		bulletbox._animating = true;
		
		switch ( effect )
		{
			case 'none':
				liwe.fx.set_opacity ( _current.id, 0 );
				liwe.fx.set_opacity ( _next.id, 100 );
				bulletbox._animating = false;
				break;
			case 'fade':
				liwe.fx.fade_in ( _next.id, bulletbox.animation_end );
				liwe.fx.fade_out ( _current.id );
				break;
			case 'r_push':
				liwe.fx.push ( _current.id, _next.id, 'right', bulletbox.animation_end );
				break;
			case 't_push':
				liwe.fx.push ( _current.id, _next.id, 'top', bulletbox.animation_end );
				break;
			case 'l_push':
				liwe.fx.push ( _current.id, _next.id, 'left', bulletbox.animation_end );
				break;
			case 'b_push':
				liwe.fx.push ( _current.id, _next.id, 'bottom', bulletbox.animation_end );
				break;
		};

	
	};

	bulletbox._roll = function ()
	{

		if ( bulletbox._active ) liwe.dom.del_class ( bulletbox._active, 'active' );
		bulletbox._pos += 1;

		if ( bulletbox._pos == bulletbox.items.count() ) bulletbox._pos = 0;

		
	 	bulletbox._active = $( 'bullet:' + bulletbox._pos );
		liwe.dom.add_class ( bulletbox._active, "active" );
		

		bulletbox.cbacks [ 'display' ] ( bulletbox.items [ bulletbox._pos ], bulletbox.display_item );


		bulletbox.t_roll = setTimeout ( function () { bulletbox._roll(); } , bulletbox._timing );
	};

	bulletbox._start_roll = function ( ev )
	{
		bulletbox.t_roll = setTimeout ( function () { bulletbox._roll(); } , bulletbox._timing );
	};


	bulletbox._stop_roll =  function ( ev )
	{
		clearTimeout ( bulletbox.t_roll );
	};

	bulletbox.toggle_controls = function ( ev )
	{

		if ( bulletbox.container._showed )
		{
			bulletbox.t_controls = setTimeout ( function ()
			{
				liwe.fx.fade_out ( bulletbox.bb.id );
			}, 1000 ) ;
		}else{
		
			clearTimeout ( bulletbox.t_controls );
			liwe.fx.fade_in ( bulletbox.bb.id );
			
		};

		bulletbox.container._showed = 1 - bulletbox.container._showed;

	};

	return bulletbox;

};



WWL.bulletbox.get_instance = function ( instance_name )
{
	return WWL.get_instance ( "bulletbox", instance_name );
};

WWL.bulletbox.display_item = function ( name, id )
{
	var g = WWL.bulletbox.get_instance ( name );
	g.display_item ( id );	
};


